/**
 * 在数组元素之间插入元素
 * eg: origin = [1,2,3]; item = 0; return => [1, 0, 2, 0, 3]
 * @param origin 源数组
 * @param item 插入元素
 * @returns
 */
export const JoinItem = <T>(origin: T[], item: T) => {
    if (!Array.isArray(origin)) return []

    if (origin.length === 0) return origin

    const arr: T[] = []

    origin.forEach((it, index) => {
        if (index === origin.length - 1) {
            arr.push(it)
        } else {
            arr.push(it, item)
        }
    })

    return arr
}

/**
 * 按数量将数组元素分组
 * eg: origin = [1,2,3,4,5,6,7]; groupCount = 3; return => [[1,2,3], [4,5,6], [7]]
 * @param origin 源数组
 * @param groupCount 每组元素数量
 * @returns
 */
export const GroupArray = <T>(origin: T[], groupCount: number) => {
    if (!Array.isArray(origin)) return []

    const arr: T[][] = []

    for (let i = 0; i < origin.length; i += groupCount) {
        const group = origin.slice(i, i + groupCount)
        arr.push(group)
    }

    return arr
}
